<?php
/**
 * @see Alinks_Menu_Abstract
 */
require_once('Alinks/Menu/Abstract.php');

/**
 * Displays the factories menu
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Factories.php 432 2008-07-13 21:00:48Z sean $
 * @package Alinks_Menu
 */
class Alinks_Menu_Factories extends Alinks_Menu_Abstract
{
	/**
	 * Inits the menu
	 *
	 * Sets up CSS/JavaScript, and processes any submitted forms.
	 */
	public static function init()
	{
		if (Alinks_Registry::get('alinks_mu') && !is_site_admin()) {
			return;
		}
		
		if (isset($_POST[Alinks_Form::FORM_ALINKS])) {
			self::_processForm();
		} else if (isset($_GET['delete'])) {
			self::_deleteFactory();
		}
	}
	
	/**
	 * Shows the factories menu
	 */
	public static function show()
	{
		$d_factories = Alinks_Registry::get('Alinks_Factory_Keeper')->fetchAll();
		$d_incompatibles = Alinks_Registry::get('Alinks_Factory_Keeper')->fetchIncompatible();
		include('AlinksMenus/Factories.php');
	}
	
	/**
	 * Processes the submitted factories form
	 */
	protected static function _processForm()
	{
		if (Alinks_Registry::get('alinks_mu') && !is_site_admin()) {
			return;
		}
		
		check_admin_referer(Alinks_Form::NONCE_ACTION);
		if (empty($_FILES['file']) || empty($_FILES['file']['name'])) {
			add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
			self::$_error_msg = 'You must upload a zip file to install.';
			return;
		}
		if ($_FILES['file']['error'] != 0) {
			add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
			self::$_error_msg = 'It looks like there was an error uploading the file. PHP returned the error code ' . $_FILES['file']['error'];
			return;
		}
		
		$factories = Alinks_Registry::get('Alinks_Factory_Keeper');
		try {
			$factories->install($_FILES['file']['tmp_name']);
		} catch (Alinks_Factory_Keeper_Exception $e) {
			add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
			self::$_error_msg = $e->getMessage();
			return;
		}
		
		add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
		self::$_success_msg = 'The factory has been installed!';
		self::$_success_title = 'Successfully installed factory';
	}
	
	/**
	 * Deletes a factory
	 */
	protected static function _deleteFactory()
	{
		if (Alinks_Registry::get('alinks_mu') && !is_site_admin()) {
			return;
		}
		
		check_admin_referer(Alinks_Form::NONCE_ACTION);
		$factories = Alinks_Registry::get('Alinks_Factory_Keeper');
		
		try {
			$factories->uninstall($_GET['delete']);
		} catch (Alinks_Factory_Keeper_Exception $e) {
			add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
			self::$_error_msg = $e->getMessage();
			return;
		}
		
		add_action('admin_notices', array('Alinks_Menu_Factories', 'adminNotices'));
		self::$_success_msg = 'The factory has been deleted!';
		self::$_success_title = 'Factory Deleted';
	}
	
	/**
	 * Returns the name of the factory
	 *
	 * If a factory URL is set, the name will be a clickable link.
	 *
	 * @param string $name The factory name
	 * @param string $url The factory URL
	 * @return string
	 */
	protected static function _getName($name, $url)
	{
		if (null !== $url) {
			$name = '<a href="' . $url . '">' . $name . '</a>';
		}
		
		return $name;
	}
	
	/**
	 * Returns the factory description
	 *
	 * Returns the factory description, and the factory authors if they
	 * are set.
	 *
	 * @param string $description The factory description
	 * @param array $authors The factory authors
	 * @return string
	 */
	protected static function _getDescription($description, $authors)
	{
		if ($authors) {
			$description .= ' by ' . self::_getAuthorsHtml($authors);
		}
		
		return $description;
	}
	
	/**
	 * Returns the HTML to display the factory authors
	 *
	 * @param array $authors The authors
	 * @return string
	 */
	protected static function _getAuthorsHtml($authors)
	{
		if (!is_array($authors)) {
			return $authors;
		}
		
		$htmls = array();
		if (isset($authors[0]) && is_array($authors[0])) {
			foreach($authors as $author) {
				$html = null;
				if (isset($author['url'])) {
					$html .= '<a href="' . $author['url'] . '">';
				}
				$html .= $author['name'];
				if (isset($author['url'])) {
					$html .= '</a>';
				}
				
				$htmls[] = $html;
			}
		} else {
			$html = null;
			if (isset($authors['url'])) {
				$html .= '<a href="' . $authors['url'] . '">';
			}
			$html .= $authors['name'];
			if (isset($authors['url'])) {
				$html .= '</a>';
			}
			
			$htmls[] = $html;
		}
		
		return join(', ', $htmls);
	}
}